package com.zhiying.controller;

import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zhiying.config.StatusEnum;
import com.zhiying.domain.CMS_User;
import com.zhiying.domain.Role;
import com.zhiying.domain.UserLoginLog;
import com.zhiying.mybatis.UserMapper;
import com.zhiying.page.BasePageDTO;
import com.zhiying.page.CmsPageResult;
import com.zhiying.page.CmsResult;
import com.zhiying.page.PageInfo;
import com.zhiying.service.LoginService;
import com.zhiying.service.MenuService;
import com.zhiying.until.RegexUtils;
import com.zhiying.until.code.CommonReturnCode;
import com.zhiying.until.code.UserReturnCode;


@Controller
@RequestMapping(value = "/administrator/info")
public  class AdministratorInfoController {

	@Autowired
	MenuService  menuService;

	//	@Autowired
	//	MenuJPA menu;
	@Autowired
	private LoginService loginService;

	@Autowired
	UserMapper userMapper;

	/**
	 * GET 管理员个人信息
	 * @return
	 */
	@RequiresPermissions("administrator:info:view")
	@GetMapping(value = "/view")
	public String getInfoPage(Model model) {

		CMS_User users=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));
		// 管理员信息
		CMS_User user = menuService.selectUserById(users.getUserId());
		model.addAttribute("user", user);

		// 管理员权限
		List<Role> roles = userMapper.listByUserIds(StatusEnum.NORMAL.getStatus(),users.getUserId());
		model.addAttribute("roles", roles);

		return "/admin/admin_user_info";
	}

	/**
	 * GET 管理员个人登录日志
	 * @return
	 */
	@RequiresPermissions("administrator:info:view")
	@GetMapping(value = "/logs")
	@ResponseBody
	public Object listLogs(PageInfo pageInfo, @RequestParam(required = false, value = "search") String search) {
		//AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		CMS_User users=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));
		//if (authorizingUser != null) {
		// 管理员日志
		BasePageDTO<UserLoginLog> basePageDTO = menuService.listByPage(users.getUserId(), pageInfo, search);
		return new CmsPageResult(basePageDTO.getList(), basePageDTO.getPageInfo().getTotal());
		/*
		 * } else { return new CmsResult(CommonReturnCode.UNAUTHORIZED); }
		 */
	}
	/**
	 * POST 修改管理员密码
	 * @return
	 */
	@RequiresPermissions("administrator:info:edit")
	@PutMapping(value = "/edit/psw")
	@ResponseBody
	public Object updatePwd(@RequestParam("nowPasswords") String nowPassword,
			@RequestParam("newPasswords") String newPassword, @RequestParam("confirmPwds") String confirmPwd) {

		if (!RegexUtils.isPassword(newPassword)) {
			return new CmsResult(UserReturnCode.PASSWORD_AUTHENTICATION_ERROR);
		}
		if (!newPassword.equals(confirmPwd)) {
			return new CmsResult(UserReturnCode.ENTERED_PASSWORDS_DIFFER);
		}
		if(menuService.selectUserByPassword(nowPassword)==null) {
			return new CmsResult(UserReturnCode.WRONG_PASSWORD);
		}
		Integer count = menuService.updateInfos(newPassword,nowPassword);
		return new CmsResult(CommonReturnCode.SUCCESS, count);
		//AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		//if (authorizingUser != null) {
		//			try {
		//				
		//			} catch (Exception e) {
		//				/*
		//				 * logger.error(e.getMessage(), e); return new CmsResult(e.getCode(),
		//				 * e.getMessage());
		//				 */
		//			}
		//		} else {
		//			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
		//		}
	}
	/**
	 * PUT 更新管理员信息
	 * @return
	 */
	@RequiresPermissions("administrator:info:edit")
	@PutMapping(value = "/edit")
	@ResponseBody
	public Object updateUser(CMS_User user) {
		/*
		 * String name=req.getParameter("userName"); System.out.println(name);
		 */
//		if (authorizingUser != null) {
		/*user.setUserId(authorizingUser.getUserId());
			user.setUpdateBy(authorizingUser.getUserName());*/
		    CMS_User users=loginService.getUserInfoByUserName((String)SecurityUtils.getSubject().getSession().getAttribute("loginName"));
		    user.setUserId(users.getUserId());
			int count = menuService.updateAdminInfoByUserId(user);
			return new CmsResult(CommonReturnCode.SUCCESS, count);
		/*} else {
			return new CmsResult(CommonReturnCode.UNAUTHORIZED);
		}*/
	}

}
